//辅助函数，处理表头使用
function initTableHeader(){
    if(self_drive == '1'){
        $('#info_fee_table th').eq(5).text('油费');
        $('#info_fee_table th').eq(6).text('过路费');
        $('#info_fee_table th').eq(7).text('里程数');
    }else{
        $('#info_fee_table th').eq(5).text('车费');
        $('#info_fee_table th').eq(6).text('船费');
        $('#info_fee_table th').eq(7).text('机票');
    }
}



//辅助函数，处理上传文件
function processUploadFile(){
    $('#upload_file_area').attr('action', url+'UploadFile.php');
    $('#uploadButton').click(function(){
        if($(this).hasClass('disabled')){
            return;
        }
        $('#upload').click();
    });

}

//辅助函数，处理是否有票的显示
function processHasReceipe(){
    $('input[name="hasReceipe"]').click(function(){
        var v = $(this).val();
        if(v == 1){
            $('#non_rece_area').hide();
        }
        else{
            $('#non_rece_area').show();
        }
    });
}

//辅助函数，处理嫌疑人预报销费用是否等于分项费用的判断
function processSuspectTotalFee(){
    var fee1 = parseFloat(processFee1($('#suspect_life_fee')));
    var fee2 = parseFloat(processFee1($('#suspect_road_fee')));
    var fee3 = parseFloat(processFee1($('#suspect_other_fee')));

    var totalFee = fee1 + fee2 + fee3;


    var curElem = $('#pre_suspect_fee');
    if($(curElem).next().hasClass('state')){
        $(curElem).next().remove();
    }

    if(totalFee != parseFloat($('#pre_suspect_fee').val())){
        $(curElem).after("<span class='state ui-state-error-text'>" +
            "<i class='ui-icon ui-icon-circle-close'></i>" +
            totalFee + "不等于预报销费用</span>");
        return false;
    }
    else{
        $(curElem).after("<span class='state'>" +
            "<i class='ui-icon ui-icon-circle-check'></i>" +
            "</span>");
        return true;
    }
}

//辅助函数，用于处理车费，船费，机票，过路费，其它费用的输入错误逻辑
function processFee1(elem){
    if($(elem).next().hasClass('state')){
        $(elem).next().remove();
    }
    if($(elem).val() == ""){
        $(elem).val(0);
        return 0;
    }

    if($(elem).val() < 0 || !checkNum($(elem).val())){
        $(elem).after("<span class='state ui-state-error-text'>" +
            "<i class='ui-icon ui-icon-circle-close'></i>" +
            "</span>");
        return -1;
    }
    else{
        $(elem).after("<span class='state'>" +
            "<i class='ui-icon ui-icon-circle-check'></i>" +
            "</span>");

        return $(elem).val();
    }
}

//辅助函数，用于处理住宿费，公杂费，伙食补助的输入错误逻辑
function processFee2(elem){
    var total_days = $('#total_days').val();
    //var stay_days = $('#stay_days').val();
    $(elem).next().html('');

    if($(elem).val() == ""){
        $(elem).val(0);
        return 0;
    }

    if($(elem).val() < 0 || !checkNum($(elem).val())){
        $(elem).next().addClass("ui-state-error-text");
        $(elem).next().html("<i class='ui-icon ui-icon-circle-close'></i>格式错误");
        return -1;
    }
    else{
        var error = false;
        if($(elem).attr('id') == 'hotel_fee'){
            if($(elem).val() > total_days * standard.hotel_max){
                $(elem).next().addClass("ui-state-error-text");
                $(elem).next().html("<i class='ui-icon ui-icon-circle-close'></i>超过报销标准");
                error = true;
            }
        }
        if($(elem).attr('id') == 'food_fee'){
            if($(elem).val() > total_days * standard.food){
                $(elem).next().addClass("ui-state-error-text");
                $(elem).next().html("<i class='ui-icon ui-icon-circle-close'></i>超过报销标准");
                error = true;
            }
        }
        if($(elem).attr('id') == 'inci_fee'){
            if($(elem).val() > total_days * standard.inci){
                $(elem).next().addClass("ui-state-error-text");
                $(elem).next().html("<i class='ui-icon ui-icon-circle-close'></i>超过报销标准");
                error = true;
            }
        }

        if(!error){
            $(elem).next().removeClass("ui-state-error-text");
            $(elem).next().html("<i class='ui-icon ui-icon-circle-check'></i>");
            return $(elem).val();
        }
        else{
            return -1;
        }
    }
}
//处理公务卡报销数据
function processFee3(elem){
    $(elem).next().html('');

    if($(elem).val() == ""){
        $(elem).val(0);
        return 0;
    }
    var total_fee = parseFloat($('#total_fee').val());
    var official_fee = parseFloat($(elem).val());
    if(official_fee > total_fee){
        $(elem).next().addClass("ui-state-error-text");
        $(elem).next().html("<i class='ui-icon ui-icon-circle-close'></i>超过总报销费用");
        return -1;
    }
    else{
        $(elem).next().removeClass("ui-state-error-text");
        $(elem).next().html("<i class='ui-icon ui-icon-circle-check'></i>");
        return official_fee;
    }
}

//辅助函数，处理公务卡报销费用的输入
function processOfficialFeeInput(elem){
    var maxVal = parseFloat($(elem).parent().next().next().find('input').val());
    var curVal = parseFloat($(elem).val());
    if(curVal > maxVal){
        $(elem).next().addClass("ui-state-error-text");
        $(elem).next().html("<i class='ui-icon ui-icon-circle-close'></i>");
        return 0;
    }
    else{
        $(elem).next().removeClass("ui-state-error-text");
        $(elem).next().html("<i class='ui-icon ui-icon-circle-check'></i>");
        return parseFloat($(elem).val());
    }

}

//辅助函数，计算公务卡实报总费用
function calcTotalOfficialFee(){
    var total_fee = processOfficialFeeInput($('#official_car_maintain_fee')) +
            processOfficialFeeInput($('#official_plane_fee')) +
            processOfficialFeeInput($('#official_hotel_fee')) +
            processOfficialFeeInput($('#official_oil_fee'));
    total_fee = total_fee.toFixed(2);
    $('#official_total_fee').val(total_fee);

}

//辅助函数，处理里程费用
function processFee4(elem){
    if($(elem).next().hasClass('state')){
        $(elem).next().remove();
    }
    if($(elem).val() == ""){
        $(elem).val(0);
        return 0;
    }

    if($(elem).val() < 0 || !checkNum($(elem).val())){
        $(elem).after("<span class='state ui-state-error-text'>" +
            "<i class='ui-icon ui-icon-circle-close'></i>" +
            "</span>");
        return -1;
    }
    else{
        $(elem).after("<span class='state'>" +
            "<i class='ui-icon ui-icon-circle-check'></i>" +
            "</span>");

        var oil_fee = parseFloat($(elem).val()) / 100 * standard.oil_count * standard.oil_price;

        oil_fee = Math.round(oil_fee * 100) / 100;

        $('#oil_fee').val(oil_fee);
        return $(elem).val();
    }
}

//辅助函数，计算总费用信息
function calcTotalFee(){
    //不变的费用
    var hotel_fee = parseFloat(processFee2($('#hotel_fee')));
    var inci_fee = parseFloat(processFee2($('#inci_fee')));
    var food_fee = parseFloat(processFee2($('#food_fee')));
    var other_other_fee = parseFloat(processFee1($('#other_other_fee')));
    var other_reward_fee = parseFloat(processFee1($('#other_reward_fee')));
    var other_suspect_fee = parseFloat(processFee1($('#other_suspect_fee')));


    var totalFee = hotel_fee + inci_fee + food_fee + other_other_fee + other_reward_fee + other_suspect_fee;

    if(self_drive == '0'){
        var car_fee = parseFloat(processFee1($('#car_fee')));
        var boat_fee = parseFloat(processFee1($('#boat_fee')));
        var flight_fee = parseFloat(processFee1($('#flight_fee')));
        totalFee += car_fee + boat_fee + flight_fee;
    }
    else{
        var pass_fee = parseFloat(processFee1($('#pass_fee')));
        var oil_fee = parseFloat($('#oil_fee').val());
      //  var oil_fee = parseFloat(processFee2($('#oil_fee')));
        var car_maintain_fee = parseFloat(processFee1($('#car_maintain_fee')));
        totalFee += pass_fee + oil_fee + car_maintain_fee;
    }

    $('#total_fee').val((Math.round(totalFee * 100) / 100).toString());
}

//辅助函数，处理费用的和输入
function processSuspectFeeInput(){
    $('#suspect_life_fee, #suspect_road_fee, #suspect_other_fee').bind('input propertychange', function(){
        processFee1($(this));
        processSuspectTotalFee();
    })
}